<template>
  <div class="app-container">
    <!-- 高级搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="部署时间">
        <el-date-picker
          v-model="daterangeDeployTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核类型" prop="bpmnType">
        <el-select v-model="queryParams.bpmnType" placeholder="请选择审核类型" clearable>
          <el-option
            v-for="dict in dict.type.flow_bpmn_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bpmnInfo:info:add']"
        >流程文件部署</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="审核类型" align="center" prop="bpmnType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flow_bpmn_type" :value="scope.row.bpmnType"/>
        </template>
      </el-table-column>
      <el-table-column label="流程名称" align="center" prop="bpmnLabel" />
      <el-table-column label="流程定义key" align="center" prop="processDefinitionKey" />
      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column label="描述信息" align="center" prop="info" />
      <el-table-column label="部署时间" align="center" prop="deployTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deployTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流程文件" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-warning"
            @click="previewXMLFile(scope.row)"
            v-hasPermi="['bpmnInfo:info:query']"
          >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="流程图" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-warning"
            @click="previewPNGFile(scope.row)"
            v-hasPermi="['bpmnInfo:info:query']"
          >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleCancel(scope.row)"
            v-hasPermi="['bpmnInfo:info:delete']"
          >撤销</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程定义明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流程类型" prop="bpmnType">
          <el-select v-model="form.bpmnType" placeholder="请选择流程类型">
            <el-option
              v-for="dict in dict.type.flow_bpmn_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程文件 ">
          <el-upload
            ref="upfile"
            action="upload"
            :on-change="uploadFile"
            style="display: inline"
            :auto-upload="false"
            :file-list="fileList"
            >
            <el-button type="success">选择文件</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="描述信息" prop="info">
          <el-input v-model="form.info" placeholder="请输入描述信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 流程文件浏览 -->
    <el-dialog :title="title" :visible.sync="xmlOpen" width="1234px"  append-to-body>
      <pre style="overflow-x: auto; overflow-y: auto;">
        <code class="hljs" v-html="highlightXml(xmlContent)"></code>
      </pre>
    </el-dialog>

    <!-- 流程图浏览 -->
    <el-dialog :title="title" :visible.sync="pngOpen" width="857px" append-to-body>
      <div align="center" v-html="pngContent"></div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, readXml } from "@/api/bpmnInfo/info";
import hljs from "highlight.js/lib/highlight";
import "highlight.js/styles/github-gist.css";
hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml"));

export default {
  name: "Info",
  dicts: ['flow_bpmn_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程定义明细表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示流程xml弹出层
      xmlOpen: false,
      // 修成定义文件内容
      xmlContent: "",
      // 是否显示流程图弹出层
      pngOpen: false,
      // 流程图文件内容
      pngContent: "",
      // 描述信息时间范围
      daterangeDeployTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bpmnType: null,
        deployTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bpmnType: [
          { required: true, message: "流程类型不能为空", trigger: "change" }
        ],
        info: [
          { required: true, message: "描述信息不能为空", trigger: "blur" }
        ]
      },
      // 单文件上传
      file: null,
      // 多文件上传
      fileList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询流程定义明细列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDeployTime && '' != this.daterangeDeployTime) {
        this.queryParams.params["beginDeployTime"] = this.daterangeDeployTime[0];
        this.queryParams.params["endDeployTime"] = this.daterangeDeployTime[1];
      }
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bpmnLabel: null,
        bpmnType: null,
        processDefinitionKey: null,
        deployTime: null,
        version: null,
        info: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDeployTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流程定义明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let formData = new FormData();
          formData.append("file", this.file.raw);
          formData.append("bpmnType", this.form.bpmnType);
          formData.append("info", this.form.info);
          formData.append("bpmnLabel", this.file.name);
          addInfo(formData).then(response => {
             this.$modal.msgSuccess("新增成功");
             this.open = false;
             this.getList();
           });
        } 
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除流程定义明细编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bpmnInfo/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "流程部署";
    },
    /** 上传文件 */
    uploadFile(file, fileList) {
        this.file = file;
        this.fileList = [file];
    },
    // 预览文件
    previewXMLFile(row) {
      readXml(row.id, "xml").then(response => {
        this.xmlContent = response;
        this.title = "流程文件预览";
        this.xmlOpen = true;
      });
    },
    // 预览流程图
    previewPNGFile(row) {
      readXml(row.id, "png").then(response => {
        this.pngContent = response;
        this.title = "流程图预览";
        this.pngOpen = true;
      });
    },
    // 高亮 xml 代码
    highlightXml(code) {
      return hljs.highlight("xml", code || "").value;
    },
    // 撤销流程定义
    handleCancel(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认撤销流程定义？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤销成功");
      }).catch(() => {});
    },
  }
};
</script>
